#!/usr/bin/env bash

java_tar=$(ls | grep '^jdk.*\.tar\.gz$')

if [ ! -e "${java_tar}" ];then
	echo "The java installation package does not exist"
	exit	
fi

read -p "Please enter the decompression directory: " java

if [ ! -d "${java}" ];then
	mkdir -p ${java}
fi

tar -zxvf ${java_tar} -C ${java}

if [ $? -ne 0 ];then
	echo "unzip error"
	exit
fi 

version=$(ls ${java} |grep '^jdk')

cat <<-EOF >>/etc/profile
export JAVA_HOME=${java}/${version}
EOF
cat <<-"EOF" >>/etc/profile
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib:$CLASSPATH
export JAVA_PATH=${JAVA_HOME}/bin:${JRE_HOME}/bin
export PATH=$PATH:${JAVA_PATH}
EOF

source /etc/profile
java
java --version
javac
